using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectSLM_Members()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_SLM_MEMBERS
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectSLM_MembersPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_SLM_MEMBERS,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListSLM_Members", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteSLM_Members(int memberid)
		{
			return Json(new{
				Success = _repSLM_Members.Delete(memberid),
				RecordCount = _repSLM_Members.SelectAll().Count,
				DeleteId = memberid
			});
		}

		public ActionResult DetailOfSLM_Members(int memberid)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_SLM_MEMBERS,
				IntID = memberid
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateSLM_Members(FormCollection forms)
		{
			string memberid = forms.GetValues("SLM_Members_MemberId")[0];
			string username = forms.GetValues("SLM_Members_UserName")[0];
			string password = forms.GetValues("SLM_Members_Password")[0];
			string salt = forms.GetValues("SLM_Members_Salt")[0];
			string email = forms.GetValues("SLM_Members_Email")[0];
			string fullname = forms.GetValues("SLM_Members_FullName")[0];
			string datecreation = forms.GetValues("SLM_Members_DateCreation")[0];
			string isactivated = forms.GetValues("SLM_Members_IsActivated")[0];
			string enablelogin = forms.GetValues("SLM_Members_EnableLogin")[0];
			string activatestring = forms.GetValues("SLM_Members_ActivateString")[0];
			string currentpage = forms.GetValues("SLM_Members_CurrentPage")[0];
			var slm_members = _repSLM_Members.SelectByID(int.Parse(memberid));
			slm_members.MemberId = int.Parse(memberid.Replace(".", "").Replace(",", ""));
			slm_members.UserName = username;
			slm_members.Password = password;
			slm_members.Salt = salt;
			slm_members.Email = email;
			slm_members.FullName = fullname;
			slm_members.DateCreation = DateTime.Now;
			slm_members.IsActivated = bool.Parse(isactivated.Replace(".", "").Replace(",", ""));
			slm_members.EnableLogin = bool.Parse(enablelogin.Replace(".", "").Replace(",", ""));
			slm_members.ActivateString = Guid.Parse(activatestring.Replace(".", "").Replace(",", ""));
			var result = _repSLM_Members.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_SLM_MEMBERS,
				CurrentPage = int.Parse(currentpage)
			};
			return PartialView("Templates/TH_ListSLM_Members", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertSLM_Members(FormCollection forms)
		{
			string username = forms.GetValues("SLM_Members_UserName")[0];
			string password = forms.GetValues("SLM_Members_Password")[0];
			string salt = forms.GetValues("SLM_Members_Salt")[0];
			string email = forms.GetValues("SLM_Members_Email")[0];
			string fullname = forms.GetValues("SLM_Members_FullName")[0];
			string datecreation = forms.GetValues("SLM_Members_DateCreation")[0];
			string isactivated = forms.GetValues("SLM_Members_IsActivated")[0];
			string enablelogin = forms.GetValues("SLM_Members_EnableLogin")[0];
			string activatestring = forms.GetValues("SLM_Members_ActivateString")[0];
			var newItem = new SLM_Members
			{
				UserName = username,
				Password = password,
				Salt = salt,
				Email = email,
				FullName = fullname,
				DateCreation = datecreation == "" ? DateTime.Now : DateTime.Parse(datecreation),
				IsActivated = bool.Parse(isactivated),
				EnableLogin = bool.Parse(enablelogin),
				ActivateString = Guid.Parse(activatestring)
			};
			var result = _repSLM_Members.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_SLM_MEMBERS,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return Json(new{
				Success = result,
				Message = "A new item has been added!",
				PartialViewHtml = RenderPartialViewToString("Templates/TH_ListSLM_Members", CreateViewModel(data))
			});
		}


	}
}